from alipay import AliPay
from alipay.utils import AliPayConfig
import time

# 使用之前配置的密钥
app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCcKTY8a4vAP0Bk
aa95LeKy0G+5mpNW5JDtD83ESxuP0xnKtmCKPG3kSHvG9oo89r4arv6xXUSV54A5
jVtCb5G0u4vjGLckSgF7PkVAdOxSQOMNbPiUrwnwDSwCJkuoNjn1YT9bhivkEl5v
FgDD48XLunuMZ/UprRInGrjHiqwkF/EmHdfvh7Wz0KvSWSsBVQR1RlHK+c21sAtC
zZt1kUsFeBK1p0GjI90DH+tClh+1rjKThOPFNtPdBfGGV3Jnvv2sMnHvbDwN9SSX
Wm0vE+TxOK3s+bflxGIbJHMGsxiZpr1wB5khk26qk3jhhLrNlho4g8ryflj2f7SX
2ZYhpJo7AgMBAAECggEBAINgqKBsWkaC8EmKIHZyhCar9VLRGOQhp0vJ3/MkvStj
Dm/cv5uM+2bzFC9vex2O+KC6WMwfC3SSLu48XmqmMo86uZV1VZ7SJCzrVpfDxo3s
yRiidP+hw1qRulSUo1IBTQGDrynw1zwpuAj9Geez2NRtL0QQsdE+Ked8M7iskeZK
aIVck8zVJIpUqxVkd/GlfsEnmWZBpoOy2I26V6wu77WPmokKTiw2iOzXksLIulKz
vcyvc4eOE4ZS64O/WCHbBRtecDo1pyScNpbVA9MD4Tu80y/j7xUJMSN+KY0KNOmF
TVT5UAxwa1+fQszhMGGtv3UKVw/phU087q+JSpFm7gECgYEA7pbcExMDiIj5kGmd
F2SI/bBMq/HgKIYd8xmfGkZTjg+1WxWBfDU2/V4CNJzCqGZ9bl8jBBW5Y9vnCVyJ
vIFDv1Uz7iTCwwWhkecuzDgxtsI4hJk6+xZlQNIarikzx9dSPy78x/hzKb6PkONw
KGlmctKL0W2m9K4wU9gfBVKfgEMCgYEAp459h3R4W6y6oRUpXj6cQblbSfZkni2F
e46SnwS3l1lPpKRJBmbrljtTgCV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4q+KTc0S
wwIDAQAB
-----END PUBLIC KEY-----"""

# 初始化Alipay对象（使用之前的公钥私钥）
alipay = AliPay(
    appid="2021005150647183",  # 您的APPID
    app_notify_url="http://localhost:1120/api/payment/alipay/notify/",  # 异步通知URL
    app_private_key_string=app_private_key_string,  # 直接使用字符串形式的私钥
    alipay_public_key_string=alipay_public_key_string,  # 直接使用字符串形式的公钥
    sign_type="RSA2",  # 使用RSA2
    debug=True,  # 沙箱模式
    config=AliPayConfig(timeout=15)  # 可选配置
)

# 生成支付参数（自动加签）
order_info = alipay.api_alipay_trade_app_pay(
    out_trade_no="order_" + str(int(time.time())),  # 使用时间戳创建唯一订单号
    total_amount="0.01",  # 订单金额
    subject="测试商品",  # 商品名称
    return_url="http://localhost:1120/api/payment/alipay/return/",  # 支付成功后跳转的页面
    notify_url="http://localhost:1120/api/payment/alipay/notify/"  # 支付结果异步通知URL
)

print("加签后的orderInfo:", order_info)